defmodule ProgContestWeb.JsonifyErrors do
  import Ecto.Changeset, only: [traverse_errors: 2]
  import ProgContestWeb.ErrorHelpers, only: [translate_error: 1]
  import Phoenix.Controller, only: [json: 2]
  import Plug.Conn

  def errors(conn, changeset, status_code \\ 400) do
    errors = traverse_errors(changeset, &translate_error/1)
    conn
    |> put_status(status_code)
    |> json(errors)
  end
end
